home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 1998 May / Macworld (1998-05).dmg / Serious Demos / Lasso 2.5 Test Drive / Lasso 2.5 CGI / Java / LassoProxy / LassoResponse.java < prev    next >
Text File  |  1997-12-12  |  10KB  |  389 lines

  1. /*
  2.     LassoResponse.java
  3.  
  4.     Response parameter from LassoProxy. While not currently enforced, all public attributes
  5.     should be treated as read-only.
  6.     
  7.     Lasso responses are normally created by LassoProxy methods; you should never have any
  8.     need to instatiate a LassoResponse directly yourself.
  9.     
  10.     The toString() method displays the raw response string returned from Lasso. This can be
  11.     useful for debugging.
  12.     
  13.     Copyright © 1996 Blue World Communications, Inc.. All rights reserved.
  14. */
  15.  
  16. import java.util.*;
  17. import java.net.*;
  18.  
  19. public class LassoResponse
  20. {
  21.     // the various data types for fields
  22.     // one of these will be returned from the getFieldType method
  23.     public static final int    NONE = 0;
  24.     public static final int    CHAR = 1;
  25.     public static final int    SHORT_INTEGER = 2;
  26.     public static final int    INTEGER = 3;
  27.     public static final int    SHORT_FLOAT = 4;
  28.     public static final int    FLOAT = 5;
  29.     public static final int    IMAGE = 6;
  30.     public static final int    DATE_TIME = 7;
  31.     public static final int    BOOLEAN = 8;
  32.     
  33.     // result codes
  34.     public static final int    NOERROR = 0;
  35.     public static final int    CONNECTION_INVALID = -609; // the database application is not open
  36.     public static final int    TIMEOUT = -1712; // the request timed out
  37.     public static final int    BAD_DATA_SUPPLIED = -17005; // for instance, an invalid date for a date field
  38.     public static final int    REQUIRED_FIELD_MISSING = -800; // data was not supplied or a required field
  39.     public static final int    MEMORY_FULL = -108; // the server ran out of memory processing the request
  40.     public static final int    NO_SUCH_OBJECT = -1728; // either the database or the layout do not exist
  41.                                         // or no records were found
  42.     
  43.     private int        fNumFields;            // number of fields in the layout
  44.     private String     fFieldNames[];        // field names
  45.     private boolean    fFieldRepeats[];        // true = repeating field
  46.     private int        fRepeatSize[];        // number of values allowed for repeating field
  47.     private boolean    fFieldNullsOK[];        // allowed to be blank when adding a new record
  48.     private char    fFieldType[];        // default field type
  49.     private String    fValueList[][];        // value list for the field for the layout
  50.     
  51.     private int        fNumFound;            // number of records in the result set
  52.     private int        fNumRecords;            // number of records retrieved
  53.     private int        fNumTotal;            // total number of records in the database
  54.     private int        fRecordID[];            // IDs of retrieved records
  55.     private int        fResultCode;        // the result. 0 (zero) is no error
  56.         
  57.     private String        fFieldValue[][][];    // indexed by: record, field, field value (multiple)
  58.     
  59.     private String         fResponse;
  60.     
  61.     public LassoResponse( String lassoResponse )
  62.     {
  63.         String            temp;
  64.         StringTokenizer tempTokenizer;
  65.         Integer            tempInt;
  66.         
  67.         fResponse = lassoResponse;
  68.         
  69.         StringTokenizer tokenizer = new StringTokenizer( lassoResponse, "\u000b" );
  70.         
  71.         fNumFields = 0;
  72.         fResultCode = 0;
  73.         
  74.         temp           = tokenizer.nextToken();
  75.         tempTokenizer = new StringTokenizer( temp, "=" );
  76.         temp           = tempTokenizer.nextToken();
  77.         
  78.         if (temp.equals("[resultcode]"))
  79.         {
  80.             tempInt = new Integer(tempTokenizer.nextToken());
  81.             fResultCode = tempInt.intValue();
  82.             
  83.             temp           = tokenizer.nextToken();
  84.             tempTokenizer = new StringTokenizer( temp, "=" );
  85.             temp           = tempTokenizer.nextToken();    
  86.         }
  87.                 
  88.         if ( temp.equals( "[numfields]" ) )
  89.         {
  90.             tempInt   = new Integer( tempTokenizer.nextToken() );
  91.             fNumFields = tempInt.intValue();
  92.         }
  93.         
  94.         if ( fNumFields <= 0 ) return;
  95.         
  96.         fFieldNames = new String[ fNumFields ];
  97.         
  98.         for ( int i = 0; i < fNumFields; i++ )
  99.         {
  100.             fFieldNames[ i ] = tokenizer.nextToken();
  101.         }
  102.         
  103.         temp           = tokenizer.nextToken();    // repeats flags and repeat sizes
  104.         tempTokenizer = new StringTokenizer( temp, "ft", true );
  105.         
  106.         fFieldRepeats = new boolean[ fNumFields ];
  107.         fRepeatSize   = new int[ fNumFields ];
  108.         
  109.         for ( int i = 0; ( i < fNumFields ) && tempTokenizer.hasMoreTokens(); i++ )
  110.         {
  111.             temp = tempTokenizer.nextToken();
  112.             
  113.             if ( temp.equals( "t" ) )
  114.             {    
  115.                 tempInt = new Integer( tempTokenizer.nextToken() );
  116.                 
  117.                 fFieldRepeats[ i ] = true;
  118.                 fRepeatSize[ i ]   = tempInt.intValue();
  119.             }
  120.             else
  121.             {
  122.                 fFieldRepeats[ i ] = false;
  123.                 fRepeatSize[ i ]   = 0;
  124.             }
  125.         }
  126.         
  127.         temp           = tokenizer.nextToken();    // nulls OK flags
  128.         tempTokenizer = new StringTokenizer( temp, "ft", true );
  129.         
  130.         fFieldNullsOK = new boolean[ fNumFields ];
  131.         
  132.         for ( int i = 0; ( i < fNumFields ) && tempTokenizer.hasMoreTokens(); i++ )
  133.         {
  134.             temp = tempTokenizer.nextToken();
  135.             
  136.             if ( temp.equals( "t" ) )
  137.             {    
  138.                 fFieldNullsOK[ i ] = true;
  139.             }
  140.             else
  141.             {
  142.                 fFieldNullsOK[ i ] = false;
  143.             }
  144.         }
  145.         
  146.         temp = tokenizer.nextToken();    // default type flags
  147.         
  148.         fFieldType = new char[ fNumFields ];
  149.         
  150.         for ( int i = 0; i < fNumFields; i++ )
  151.         {
  152.             fFieldType[ i ] = temp.charAt( i );
  153.         }
  154.         
  155.         // extract value lists
  156.         
  157.         fValueList = new String[ fNumFields ][];
  158.         
  159.         for ( int i = 0; i < fNumFields; i++ )
  160.         {
  161.             tempInt       = new Integer( tokenizer.nextToken() );        // number of list values
  162.             int numValues = tempInt.intValue();
  163.             
  164.             if ( numValues > 0 )
  165.             {
  166.                 fValueList[ i ] = new String[ numValues ];
  167.                 
  168.                 for ( int j = 0; j < numValues; j++ )
  169.                 {
  170.                     fValueList[ i ][ j ] = tokenizer.nextToken();
  171.                 }
  172.             }
  173.         }
  174.         
  175.         // extract record data
  176.         
  177.         fNumRecords = 0;
  178.         
  179.         temp           = tokenizer.nextToken();
  180.         tempTokenizer = new StringTokenizer( temp, "=" );
  181.         temp           = tempTokenizer.nextToken();
  182.         
  183.         if ( temp.equals( "[numreturned]" ) )
  184.         {
  185.             tempInt    = new Integer( tempTokenizer.nextToken() );
  186.             fNumRecords = tempInt.intValue();
  187.         }
  188.         
  189.         if ( fNumRecords <= 0 ) return;
  190.         
  191.         fRecordID = new int[ fNumRecords ];
  192.         
  193.         for ( int i = 0; i < fNumRecords; i++ )
  194.         {
  195.             tempInt       = new Integer( tokenizer.nextToken() );
  196.             fRecordID[ i ] = tempInt.intValue();
  197.         }
  198.         
  199.         fFieldValue = new String[ fNumRecords ][ fNumFields ][];
  200.         
  201.         for ( int i = 0; i < fNumRecords; i++ )
  202.         {
  203.             for ( int j = 0; j < fNumFields; j++ )
  204.             {
  205.                 temp = tokenizer.nextToken();
  206.                 
  207.                 // temp contains field value; multiple values, such as for value lists, relational
  208.                 // fields and repeating fields, are separated by the character "\u000c"; a null
  209.                 // value is indicated by the character "\u0007"
  210.                 
  211.                 tempTokenizer = new StringTokenizer( temp, "\u000c" );
  212.                 
  213.                 int numValues = tempTokenizer.countTokens();
  214.  
  215.                 fFieldValue[ i ][ j ] = new String[ numValues ];
  216.  
  217.                 for ( int k = 0; k < numValues; k++ )
  218.                 {
  219.                     temp = tempTokenizer.nextToken();
  220.                     
  221.                     if ( temp.charAt( 0 ) == '\u0007' )        // null value
  222.                     {
  223.                         fFieldValue[ i ][ j ][ k ] = new String();    // empty String
  224.                     }
  225.                     else
  226.                     {
  227.                         fFieldValue[ i ][ j ][ k ] = temp;
  228.                     }
  229.                 }
  230.             }
  231.         }
  232.         
  233.         fNumFound = 0;
  234.         
  235.         temp           = tokenizer.nextToken();
  236.         tempTokenizer = new StringTokenizer( temp, "=" );
  237.         temp           = tempTokenizer.nextToken();
  238.         
  239.         if ( temp.equals( "[numfound]" ) )
  240.         {
  241.             tempInt  = new Integer( tempTokenizer.nextToken() );
  242.             fNumFound = tempInt.intValue();
  243.         }
  244.         
  245.         fNumTotal = 0;
  246.         
  247.         temp           = tokenizer.nextToken();
  248.         tempTokenizer = new StringTokenizer( temp, "=" );
  249.         temp           = tempTokenizer.nextToken();
  250.         
  251.         if ( temp.equals( "[numtotal]" ) )
  252.         {
  253.             tempInt  = new Integer( tempTokenizer.nextToken() );
  254.             fNumTotal = tempInt.intValue();
  255.         }
  256.                 
  257.     }
  258.     
  259.     // returns all the fields and their values for a particular record
  260.     public final String[][] recordData(int i)
  261.     {
  262.         return fFieldValue[i];
  263.     }
  264.     
  265.     // returns all the values for a particular field of a particular record
  266.     public final String[] fieldData(int recordNum, int fieldNum)
  267.     {
  268.         return recordData(recordNum)[fieldNum]; 
  269.     }
  270.     
  271.     // returns a particular value, of a particular field, of a particular record
  272.     public final String fieldValue(int recordNum, int fieldNum, int valueNum)
  273.     {    
  274.         return fieldData(recordNum, fieldNum)[valueNum];
  275.     }
  276.     
  277.     // returns the total number of records found
  278.     // not to be confused with numRecords()
  279.         // which returns the number of records retreived
  280.     public int numFound()
  281.     {    return fNumFound;    }
  282.     
  283.     // the number of records which were retreived
  284.     public int numRecords()
  285.     {    return fNumRecords;    }
  286.     
  287.     // total number of records in the database
  288.     public int numTotal()
  289.     {    return fNumTotal;    }
  290.     
  291.     // returns the id of the specified record
  292.     public int recordID(int i)
  293.     {    return fRecordID[i];    }
  294.     
  295.     // returns the number of fields in the selected layout
  296.     public int numFields()
  297.     {    return fNumFields;    }
  298.     
  299.     // returns all the field names
  300.     public String[] fieldNames()
  301.     {    return fFieldNames;    }
  302.     
  303.     // returns the name of a particular field
  304.     public String fieldName(int i)
  305.     {    return fFieldNames[i];    }
  306.     
  307.     // returns true if a particular field repeats
  308.     public boolean fieldRepeats(int i)
  309.     {    return fFieldRepeats[i];    }
  310.     
  311.     // returns the maxumun number of repeat values for a particular field
  312.     public int repeatSize(int i)
  313.     {    return fRepeatSize[i];        }
  314.     
  315.     // returns true of false depending on if the field can be blank
  316.     public boolean fieldNullsOK(int i)
  317.     {    return fFieldNullsOK[i];    }
  318.     
  319.     // returns the value list associated with a particular field
  320.     public String[] fieldValueList(int i)
  321.     {    return fValueList[i];        }
  322.     
  323.     // returns the result code for this response 0 (zero) is no error
  324.     public int resultCode()
  325.     {    return fResultCode;    }
  326.     
  327.     public int fieldIndex( String fieldName ) throws FieldNotFoundException
  328.     {
  329.         for ( int i = 0; i < fNumFields; i++ )
  330.         {
  331.             if ( this.fFieldNames[ i ].equals( fieldName ) )
  332.                 return i;
  333.         }
  334.         
  335.         throw ( new FieldNotFoundException( fieldName ) );
  336.     }
  337.     
  338.     public boolean hasValueList( String fieldName )
  339.     {
  340.         try
  341.         {
  342.             int fieldIndex = fieldIndex( fieldName );
  343.             
  344.             return ( ( fValueList[ fieldIndex ] != null ) &&
  345.                      ( fValueList[ fieldIndex ].length > 0 ) );
  346.         }
  347.         catch( FieldNotFoundException e )
  348.         {
  349.             System.out.println( "Exception: " + e );
  350.             return false;
  351.         }
  352.     }
  353.     
  354.     public int fieldType(int index)
  355.     {
  356.         switch(fFieldType[index])
  357.         {
  358.             case 'b':    return BOOLEAN;
  359.             case 'c':    return CHAR;
  360.             case 's':    return SHORT_INTEGER;
  361.             case 'l':    return INTEGER;
  362.             case 'f':    return SHORT_FLOAT;
  363.             case 'd':    return FLOAT;
  364.             case 'p':    return IMAGE;
  365.             case 't':    return DATE_TIME;
  366.         }
  367.         return NONE;
  368.     }
  369.     
  370.     public String toString()
  371.     {
  372.         return fResponse;
  373.     }
  374. }
  375.  
  376. class FieldNotFoundException extends Exception
  377. {
  378.     private String fieldName;
  379.     
  380.     public FieldNotFoundException( String theName )
  381.     {
  382.         fieldName = theName;
  383.     }
  384.     
  385.     public String toString()
  386.     {
  387.         return ( "Field Not Found: " + fieldName );
  388.     }
  389. }